package com.paul.sys.controller.back;

import com.paul.sys.model.Symenu;
import com.paul.sys.model.Syrole;
import com.paul.sys.service.SymenuService;
import com.paul.sys.service.SyroleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created by yangjianbo on 2015/10/9.
 */
@Controller
@RequestMapping("back/menu/")
public class MenuController {
    @Autowired
    private SymenuService symenuService;
    @Autowired
    private SyroleService syroleService;

    /**
     * 返回菜单管理视图
     * @return
     * @throws Exception
     */
    @RequestMapping("ismenu/menutree")
    public ModelAndView menuTree() throws Exception {

        ModelAndView view = new ModelAndView("back/symenu/menu-tree");
        List<Syrole> rolelist=this.syroleService.listRole(null);
        view.addObject("rolelist",rolelist);
        return view;
    }

    /**
     * 返回菜单添加视图
     * @param punid
     * @return
     * @throws Exception
     */
    @RequestMapping("add_page{punid}")
    public ModelAndView menuAddPage(@PathVariable String punid) throws Exception {
        ModelAndView view = new ModelAndView("back/symenu/menu-add");
        view.addObject("punid",punid);
        return view;
    }
    /**
     * 返回节点编辑视图
     * @param unid
     * @return
     * @throws Exception
     */
    @RequestMapping("edit_page{unid}")
    public ModelAndView menuEditPage(@PathVariable String unid) throws Exception {
        ModelAndView view = new ModelAndView("back/symenu/menu-edit");
        Symenu symenu=this.symenuService.selectByUnid(unid);
        view.addObject("unid",unid);
        view.addObject("symenu",symenu);
        return view;
    }
    /**
     * 添加菜单节点
     * @param symenu
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "doadd",method = RequestMethod.POST)
    public Map<String,Object> menuAdd(Symenu symenu) throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        String unid = UUID.randomUUID().toString();
        symenu.setUnid(unid);
        symenu.setMenusno(10);
        symenu.setMenutype("1");
        if(symenu.getMenuenable()==null){
            symenu.setMenuenable("0");
        }
        int n=this.symenuService.insert(symenu);
        result.put("status",0);
        result.put("unid",n);
        return result;
    }

    /**
     * 删除菜单节点
     * @param unid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "dodel",method = RequestMethod.POST)
    public Map<String,Object> menuDel(String unid) throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        this.symenuService.deleteMenutree(unid);
        result.put("status",0);
        result.put("unid",unid);
        return result;
    }

    /**
     * 更新菜单节点
     * @param symenu
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "doupdate",method = RequestMethod.POST)
    public Map<String,Object> menuUpdate(Symenu symenu) throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        symenuService.updateByPrimaryKey(symenu);
        result.put("status",0);
        return result;
    }
    /**
     * 返回所有菜单数据，返回json
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getAllMenu")
    public Map<String,Object> getAllMenu() throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        List<Map> list=symenuService.getAllTree();
        List<Map> list2=new ArrayList<Map>();
        Map<String,Object> map=null;
        for(int i=0;i<list.size();i++){
            map=new HashMap<String, Object>();
            map.put("id",list.get(i).get("id"));
            map.put("pId",list.get(i).get("pId"));
            map.put("name",list.get(i).get("name"));
            list2.add(map);
        }
        result.put("status",0);
        result.put("data",list2);
        return  result;
    }

    /**
     * 得到该菜单节点的角色关系
     * @param unid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "rolelink",method = RequestMethod.GET)
    public Map<String,Object> getRoleLink(@RequestParam String unid) throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        List<Map> list=this.symenuService.getRoleLink(unid);
        result.put("status",0);
        result.put("data",list);
        return result;
    }

    /**
     * 绑定或者取消角色关联
     * @param roleunid
     * @param menuunid
     * @param enable
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "dorolelink",method = RequestMethod.POST)
    public Map<String,Object> doRoleLink(@RequestParam String roleunid,@RequestParam String menuunid,@RequestParam String enable) throws Exception {
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("roleunid",roleunid);
        param.put("menuunid",menuunid);
        param.put("enable",enable);
        int res=0;
        if(enable.equals("1")){
           res=symenuService.linkRole(param);
        }
        else{
            res=symenuService.unlinkRole(param);
        }
        result.put("status",res);
        return result;
    }
}


